/*
 * Created on May 7, 2005
 *
 */
package org.mushroomdb.query;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.mushroomdb.engine.EvaluationEngine;

/**
 * @author Matias
 *
 */
public abstract class Query {
	private Set criterias;
	private Criteria defaultCriteria;
	private Criteria mainCriteria;

	/**
	 * Constructor default.
	 */
	public Query() {
		this.criterias = new LinkedHashSet();
		this.defaultCriteria = new Criteria(this);
		this.criterias.add(this.defaultCriteria);
	}
	
	/**
	 * Construye un nuevo criterio para esta consulta.
	 * @return
	 */
	public Criteria getNewCriteria() {
		Criteria criteria = new Criteria(this);
		this.criterias.remove(this.defaultCriteria);
		this.criterias.add(criteria);
		if (this.mainCriteria == null) {
			this.mainCriteria = criteria;
		}
		return criteria;
	}
	
	/**
	 * Devuelve los criterios de esta consulta.
	 * @return
	 */
	public Iterator getCriterias() {
		return this.criterias.iterator();
	}

	/**
	 * Setea el criterio principal de esta Query.
	 * @param criteria
	 */
	public void setMainCriteria(Criteria criteria) {
		if (!this.criterias.contains(criteria)) {
			throw new IllegalArgumentException();
		}
		this.mainCriteria = criteria;
	}
	
	/**
	 * Devuelve el criterio principal de esta Query.
	 * @return
	 */
	public Criteria getMainCriteria() {
		if (this.mainCriteria == null) {
			return this.defaultCriteria;
		} else {
			return this.mainCriteria;
		}
	}
	
	/**
	 * Executes this query and returns the results.
	 * @param evaluationEngine the EvaluationEngine to be used for the execution.
	 * @return an object which must be known by the caller.
	 */
	public abstract Object execute(EvaluationEngine evaluationEngine);
}
